<?php 
session_start();
ini_set('max_execution_TIME', 600);
include("dbconfig.php");

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
mysql_select_db($database) or die("Error conecting to db.");

$curr_tab_no=8;

$type=$_GET["type"];
$callback = $_GET['callback'];
if (!preg_match('/^[a-zA-Z0-9_]+$/', $callback)) {
	die('Invalid callback name');
}

$db_table = mysql_list_tables($database);
$target_table=array();
while($row = mysql_fetch_row($db_table)){
	$tb_name=$row[0];
	$pos = strpos($tb_name, "scanner_");
	if($pos!==false){
		$target_table[] = $tb_name;
	}
}
if($type=='active')
{
	
	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name;
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='80443')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where port = '80#443'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='80')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where port = '80'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='443')
{


	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where port = '443'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='22')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where port = '22'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='200')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where code = '200'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='400')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where code like '4%'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='500')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where code like '5%'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='errorstatus')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where code = '0'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}
if($type=='otherstatucode')
{

	$i=0;
	$res=array();
	foreach($target_table as &$tb_name)
	{
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1])*1000;
		$SQL ="select count(*) as c from ".$tb_name." where code != '0' and code!='' and code != '200' and code not like '5%' and code not like '4%'";
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	  	$count=$row[c];
		$res[]="[$tb_time,$count]";
		$i++;
		if($i==$curr_tab_no){
			break;
		}

	}
	echo $callback ."([\n" . join(",\n", $res) ."\n]);";

}


mysql_close($db);
?>
